package com.project.card;

import static com.project.card.util.UrlUtil.SERVER_URL;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    
    private SharedPreferences sp;
    private EditText editCode;
    private EditText editPwd;
    private Button btn_confirm;
    private JsonNode jsonNode;

    public static final ObjectMapper JSONMAPPER = new ObjectMapper()
            .enable(JsonParser.Feature.IGNORE_UNDEFINED)
            .enable(JsonParser.Feature.ALLOW_COMMENTS)
            .enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES)
            .enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES)
            .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        editCode=findViewById(R.id.login_edit_code);
        editPwd=findViewById(R.id.login_edit_pwd);
        btn_confirm = findViewById(R.id.btn_login);
        OkHttpClient client = new OkHttpClient();
        btn_confirm.setOnClickListener(v ->{
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try{
                        FormBody.Builder params = new FormBody.Builder();
                        params.add("code",editCode.getText().toString());
                        params.add("pwd",editPwd.getText().toString());
                        Log.d("spe66",editCode.getText().toString());
                        Request request = new Request.Builder()
                                .url(SERVER_URL+"user/login")
                                .post(params.build())
                                .build();
                        Response response = client.newCall(request).execute();
                        String responseData = response.body().string();
                        jsonNode = JSONMAPPER.readTree(responseData);
                        String phone = jsonNode.get("data").get("phone").asText();
                        String code = jsonNode.get("data").get("code").asText();
                        Integer balance = jsonNode.get("data").get("balance").asInt();
                        Integer id = jsonNode.get("data").get("id").asInt();
                        Log.d("spe66",responseData);
                        Log.d("spe66","code"+code);
                        sp = getSharedPreferences("current_user",MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        editor.putBoolean("login",true);
                        editor.putString("name", phone);
                        editor.putString("code", code);
                        editor.putInt("balance", balance);
                        editor.putInt("id",id);
                        editor.commit();
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }).start();
            finish();
//            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//            startActivity(intent);
        });
        
    }
}